package arabicScrabble;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


public class ComputerPlayer extends Player {
	int turnCount;
	public LinkedList<String> words = new LinkedList<String>();
	boolean khalasfound = false;
	Square[][] board;
	Dictionary dictionary;
	private MainApp mainApp;

	/**
	 * Constructor.
	 * 
	 * @param playerID
	 *            is unique String representation of a player needed for Super
	 *            Constructor.
	 * @param bag
	 *            the bag of the computer-player.
	 * @param playerNumber
	 *            index of player.
	 * @param board
	 *            is the square board on which users play.
	 * @param dictionary
	 *            is the dictionary used for checking for the validity of words.
	 * @param ma
	 *            the main app instance running this player.
	 */
	public ComputerPlayer(String playerID, Bag bag, int playerNumber,
			Square[][] board, Dictionary dictionary, MainApp ma) {
		super(playerID, bag, playerNumber, 1);
		this.board = board;
		this.dictionary = dictionary;
		this.mainApp = ma;
		type = 1;
	}

	
	
	public class Vword{
		public ArrayList<LetterIndex> word;
		public char c;
		public String fullWord;
		public Vword(){
			word=new ArrayList<LetterIndex>();
			
		}
		
	}
	
	/**
	 * It gets all the validWords in form of List<List<LetterIndex>> using
	 * getValidWordsOfRackPermutationsWithWordsOnBoard() Then it loop on which
	 * List<LetterIndex> as a valid word(but in the form of a list). Then it
	 * starts to extract the letters and place them in the right index on the
	 * board & check how much score will it get by calling calculateScoreAI().
	 * Then the score is saved and the maximum is calculated. Then the word got
	 * the maximum score will be played on the board.
	 * 
	 * 
	 */
	public void playMaxScoreFormedWord() {
		
		ArrayList<Vword> validWords = getValidWordsOfRackPermutationsWithWordsOnBoard();

		int maxScore = 0;
		Vword maxWord = new Vword();
		int temp1 = 0;
		int temp2 = 0;
		int temp3 = 0;
		int temp4 = 0;
		int temp5 = 0;
		int temp6 = 0;

		temp1 = this.numberOfCharactersInBoard();

		for (Vword validWord : validWords) {
			temp5 = validWord.word.size();
			Square t = this.getCommonSquare(validWord.word);
			if (t != null) {
				System.out.println("check this letter: " + t);
			}
			ArrayList<Letter> word = new ArrayList<Letter>();
			for (LetterIndex li : validWord.word){
				word.add(li.prepare(board));
			}// end for
			temp2 = this.numberOfCharactersInBoard();
			try {
				String calcScore=mainApp.calculateScoreAI(word);
				validWord.c=calcScore.charAt(0);
				int score =Integer.parseInt(calcScore.substring(1));
				temp3 = this.numberOfCharactersInBoard();
				for (LetterIndex li : validWord.word) {
					// li.letter.square.letter=null;
					li.clear(board);

				}// end for
				temp4 = this.numberOfCharactersInBoard();
				temp6 = validWord.word.size();
				if (temp2 != temp3 || temp1 != temp4) {
					System.out.println(temp1 + "," + temp2 + "," + temp3 + ","
							+ temp4);
					System.out.println("Turn: " + this.mainApp.turnCount);
				}
				if (score > maxScore) {
					maxScore = score;
					maxWord = validWord;

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}// end for

		if (maxWord != null) {
			super.score = maxScore + super.score;
			// System.out.println("Max Word: " + maxWord + ", Max Score: "
			// + maxScore);
			 
			
			//LetterIndex blank = null;
			
			//mainApp.setBlankTile(word,blank);
			
			
			for (LetterIndex li : maxWord.word) {
				if(li.letter.blank){
					//	blank=li;
						li.letter.symbol=maxWord.c;
						li.letter.blank=false;
					}
				
				
				mainApp.insertTile(li.letter, board[li.i][li.j]);
			}// end for

			mainApp.pass=0;
		} else {
			System.out.println("no words");
			mainApp.pass++;
		}
	}

	private Square getCommonSquare(ArrayList<LetterIndex> validWord) {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				for (LetterIndex letterIndex : validWord) {
					if (board[i][j] == letterIndex.letter.square
							|| board[i][j].letter == letterIndex.letter) {
						return board[j][j];
					}
				}
			}
		}
		return null;
	}

	public int numberOfCharactersInBoard() {
		int n = 0;
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				if (board[i][j].letter != null) {
					n++;
				}
			}
		}
		return n;
	}

	/**
	 * It gets the permutations of the rack (see getRackPermutations()), the
	 * current words on board(getWordsOnBoard2()) and then start to make all the
	 * possible combinations between the permutations and each of the words
	 * currently on board so it checks if permutation1 + word + permutation2,
	 * permutation2+ word + permutation1, permutation + word, & word +
	 * permutation can be placed on the board and won't throw out of bound
	 * exception & if they exist in the dictionary.
	 * 
	 * Then it converts them to List<List<LetterIndex>> using
	 * convertStrToLetterIndexList()
	 * 
	 * @return List<List<LetterIndex>> See convertStrToLetterIndexList()
	 * 
	 */

	public ArrayList<Vword> getValidWordsOfRackPermutationsWithWordsOnBoard() {
		List<String> rackPermutations = getRackPermutations();
		List<WordOnBoard> wordsOnBoard = getWordsOnBoard2();
		List<validWordOnBoard> validWords = new ArrayList<validWordOnBoard>();
		char[] temp1 = getRackasString().toCharArray();
		Arrays.sort(temp1);
		String rackStr = new String(temp1);
		String permutation = "";
		/**************** Testing ***********************/
		// rackPermutations = rackPermutations.subList(0, 5);
		/**************** End Testing *******************/
		for (int i = 0; i < wordsOnBoard.size(); i++) {

			for (int j = 0; j < rackPermutations.size(); j++) {

				if (wordsOnBoard.get(i).addAfter(rackPermutations.get(j))) {

					validWords.add(new validWordOnBoard("",
							wordsOnBoard.get(i), rackPermutations.get(j)));
					// System.out.println("word + rack: " + wordsOnBoard.get(i)
					// + ", " + rackPermutations.get(j));
				}
				if (wordsOnBoard.get(i).addBefore(rackPermutations.get(j))) {
					validWords.add(new validWordOnBoard(
							rackPermutations.get(j), wordsOnBoard.get(i), ""));
					// System.out.println("rack + word : "
					// + rackPermutations.get(j) + ", "
					// + wordsOnBoard.get(i));
				}
				for (int k = 0; k < rackPermutations.size(); k++) {
					if (j == k) {
						continue;
					}
					temp1 = (rackPermutations.get(j) + rackPermutations.get(k))
							.toCharArray();
					if (temp1.length > rackStr.length()) {
						continue;
					}
					Arrays.sort(temp1);
					permutation = new String(temp1);

					if (!rackStr.contains(permutation)) {
						continue;
					}
					if (wordsOnBoard.get(i).addBeforeAndAfter(
							rackPermutations.get(j), rackPermutations.get(k))) {
						validWords.add(new validWordOnBoard(rackPermutations
								.get(j), wordsOnBoard.get(i), rackPermutations
								.get(k)));
						// System.out.println("rackj + word + rackk: "
						// + rackPermutations.get(j) + ", "
						// + wordsOnBoard.get(i) + ", "
						// + rackPermutations.get(k));
					}
					if (wordsOnBoard.get(i).addBeforeAndAfter(
							rackPermutations.get(k), rackPermutations.get(j))) {
						validWords.add(new validWordOnBoard(rackPermutations
								.get(k), wordsOnBoard.get(i), rackPermutations
								.get(j)));
						// System.out.println("rackk + word + rackj: "
						// + rackPermutations.get(k) + ", "
						// + wordsOnBoard.get(i) + ", "
						// + rackPermutations.get(j));
					}

				}// end of loop to check for pr + wob + pr

			}// end inner loop
		}// end outer loop

		Collections.sort(validWords);
		/**************** Testing ***********************/
		// validWords = validWords.subList(0, 5);
		/**************** End Testing *******************/
		return convertStrToLetterIndexList(validWords, wordsOnBoard);

	}

	/**
	 * Produces ArrayList<ArrayList<LetterIndex>> Given List<ValidWordOnBoard>
	 * See getValidWordsOfRackPermutationsWithWordsOnBoard() List<WordOnBoard>.
	 * see getWordsOnBoard2 It converts the validWords into List of Lists of
	 * Letters to prepare it for calling the caluculateScoreAI.
	 * 
	 * each ValidWordOnBoard contains the current wordOnBoard object & the
	 * prefix and/or suffix added using the rack permutations. So it creates new
	 * list for each word and add to it the letters played from the rack (the
	 * actually Letter object) and an index for this letter where should it be
	 * placed encapsulated in LetterIndex object.
	 * 
	 * @param validWords
	 * @param wordsOnBoard
	 * @return
	 * 
	 */
	private ArrayList<Vword> convertStrToLetterIndexList(
			List<validWordOnBoard> validWords, List<WordOnBoard> wordsOnBoard) {
		ArrayList<Vword> l = new ArrayList<Vword>();
		for (validWordOnBoard validWord : validWords) {
			Vword word = new Vword();
			WordOnBoard wordOnBoard = validWord.word;
			int i = wordOnBoard.i;
			int j = wordOnBoard.j;
			char direction = wordOnBoard.direction;
			String prefix = validWord.prefix;
			String suffix = validWord.suffix;
			List<Letter> tempRack = new ArrayList<Letter>(getRack().letters);
			for (int k = 0; k < prefix.length(); k++) {
				for (int rackIndex = 0; rackIndex < tempRack.size(); rackIndex++) {
					Letter letter = tempRack.get(rackIndex);
					if (letter.symbol == prefix.charAt(k)) {
						tempRack.remove(rackIndex);
						LetterIndex tempLetter = new LetterIndex(letter,
								direction == 'h' ? i : i
										- (prefix.length() - k),
								direction == 'v' ? j : j
										- (prefix.length() - k));
						/*
						 * tempLetter.prepare(board); tempLetter.clear(board);
						 */
						word.word.add(tempLetter);
						break;
					} // endif
				} // endfor rackindex
			} // endfor k
			for (int k = 0; k < suffix.length(); k++) {
				for (int rackIndex = 0; rackIndex < tempRack.size(); rackIndex++) {
					Letter letter = tempRack.get(rackIndex);
					if (letter.symbol == suffix.charAt(k)) {
						tempRack.remove(rackIndex);
						LetterIndex tempLetter = new LetterIndex(letter,
								direction == 'h' ? i : i
										+ wordOnBoard.word.length() + k,
								direction == 'v' ? j : j
										+ wordOnBoard.word.length() + k);
						/*
						 * tempLetter.prepare(board); tempLetter.clear(board);
						 */
						word.word.add(tempLetter);
						break;
					} // endif
				} // endfor rackindex
			} // endfor k
			if (!word.word.isEmpty()) {
				l.add(word);
			}
			// }// end for wordonboard
		}// end for validword

		return l;
	}

	/**
	 * Computes permutations of the input String <strong>in</strong> of length
	 * <strong>inputLength - level</strong> so it computes (inputLength P
	 * (inputLength - level))
	 * 
	 * @param in
	 * @param outputString
	 * @param used
	 * @param inputlength
	 * @param level
	 * @param permutations
	 * 
	 * 
	 */
	private void doPermute(char[] in, StringBuffer outputString,
			boolean[] used, int inputlength, int level,
			ArrayList<String> permutations) {
		if (level == inputlength) {
			permutations.add(outputString.toString());
			return;
		}

		for (int i = 0; i < inputlength; ++i) {

			if (used[i])
				continue;

			outputString.append(in[i]);
			used[i] = true;
			doPermute(in, outputString, used, inputlength, level + 1,
					permutations);
			used[i] = false;
			outputString.setLength(outputString.length() - 1);
		}
	}

	/**
	 * 
	 * @return ArrayList<String> permutations of length 1, 2, 3, 4 from the
	 *         characters in the rack.
	 * 
	 * 
	 * 
	 */
	public ArrayList<String> getRackPermutations() {
		String input = getRackasString();
		int inputLength = input.length();
		boolean[] used = new boolean[inputLength];
		StringBuffer outputString = new StringBuffer();
		char[] in = input.toCharArray();

		ArrayList<String> permutations = new ArrayList<String>();
		/*
		 * if (turnCount < 5) { for (int i = 0; i < 2; i++) doPermute(in,
		 * outputString, used, inputLength, 0, permutations);
		 * 
		 * } else {
		 */
		for (int i = 3; i < inputLength; i++)
			doPermute(in, outputString, used, inputLength, i, permutations);
		/* } */

		return permutations;
	}

	// TODO Auto-generated constructor stub

	public void printRack() {

		// ill print the rack
		System.out.println("---RACK-----CONTENTS*---");
		for (int i = 0; i < this.rack.letters.size(); i++) {
			System.out.println(this.rack.letters.get(i).symbol);

		}
		System.out.println("END OF---RACK-----CONTENTS*---");

	}

	/**
	 * 
	 * @return List<WordOnBoard> of words currently on the board, vertically &
	 *         horizontaly. the WordOnBoard object contains the word itself,
	 *         postion(i,j) number of cells after & number of cells before the
	 *         word.
	 * 
	 */
	private List<WordOnBoard> getWordsOnBoard2() {
		ArrayList<WordOnBoard> alreadyFormedWords = new ArrayList<WordOnBoard>();

		// horizontal words.
		for (int i = 0; i < this.board.length; i++) {
			String word = "";
			char direction = 'h';
			boolean isCollectingWord = false;
			int index1 = 0, index2 = 0;
			for (int j = 0; j < this.board[0].length; j++) {
				if (board[i][j] != null && board[i][j].letter != null) {
					if (!isCollectingWord) {
						index1 = i;
						index2 = j;
						isCollectingWord = true;
					} // endif !isCollectingWord
					word += board[i][j].letter.symbol;
				} /* endif board[i][j] != null && board[i][j].letter != null */
				else if (isCollectingWord) {
					if (!word.isEmpty()) {
						alreadyFormedWords.add(new WordOnBoard(word, index1,
								index2, direction, dictionary, getCellsBefore(
										index1, index2 - 1, direction),
								getCellsAfter(index1, index2 + 1, direction)));
					}
					isCollectingWord = false;
					word = "";
				} // end elseif isCollectiongWord
			} // endfor j
		} // endfor i

		for (int j = 0; j < this.board[0].length; j++) {
			String word = "";
			char direction = 'v';
			boolean isCollectingWord = false;
			int index1 = 0, index2 = 0;
			for (int i = 0; i < this.board.length; i++) {
				if (board[i][j] != null && board[i][j].letter != null) {
					if (!isCollectingWord) {
						index1 = i;
						index2 = j;
						isCollectingWord = true;
					} // endif !isCollectingWord
					word += board[i][j].letter.symbol;
				} /* endif board[i][j] != null && board[i][j].letter != null */
				else if (isCollectingWord) {
					if (!word.isEmpty()) {
						alreadyFormedWords.add(new WordOnBoard(word, index1,
								index2, direction, dictionary, getCellsBefore(
										index1 - 1, index2, direction),
								getCellsAfter(index1 + 1, index2, direction)));
					}
					isCollectingWord = false;
					word = "";
				} // end elseif isCollectiongWord
			}
		}

		return alreadyFormedWords;
	}

	/**
	 * 
	 * @param index1
	 * @param index2
	 * @param direction
	 * @return
	 * 
	 */
	private int getCellsBefore(int index1, int index2, char direction) {
		int before = 0;
		int i = index1;
		int j = index2;
		while (i > -1 && j > -1) {
			if (board[i][j] != null && board[i][j].letter != null) {
				break;
			}
			before++;
			switch (direction) {
			case 'h':
				j--;
				break;
			case 'v':
				i--;
			default:
				break;
			}
		} // end while
		return before;
	}

	/**
	 * 
	 * @param index1
	 * @param index2
	 * @param direction
	 * @return
	 * 
	 */
	private int getCellsAfter(int index1, int index2, char direction) {
		int after = 0;
		int i = index1;
		int j = index2;
		while (i < board.length && j < board[0].length) {
			if (board[i][j] != null && board[i][j].letter != null) {
				break;
			}
			after++;
			switch (direction) {
			case 'h':
				j++;
				break;
			case 'v':
				i++;
			default:
				break;
			}

		} // end while
		return after;
	}

	private List<WordOnBoard> getWordsOnBoard() {
		ArrayList<WordOnBoard> alreadyFormedWords = new ArrayList<WordOnBoard>();

		// horizontal words.
		for (int i = 0; i < this.board.length; i++) {
			String word = "";
			int index1 = 0, index2 = 0, before = 0, after = 0;
			char direction = 0;
			boolean isCollectingWord = false;

			for (int j = 0; j < this.board[0].length; j++) {
				if (board[i][j] != null && board[i][j].letter != null) {
					if (!isCollectingWord) {
						if (!word.isEmpty()) {
							alreadyFormedWords
									.get(alreadyFormedWords.size() - 1).numberOfEmptyCellsAfter = after;
						}
						before = 0;
						after = 0;
						index1 = i;
						index2 = j;
						direction = 'h';
						isCollectingWord = true;
						word = "";
					}
					word += board[i][j].letter.symbol;
				} else {
					if (isCollectingWord) {
						if (!word.isEmpty()) {
							alreadyFormedWords.add(new WordOnBoard(word,
									index1, index2, direction, dictionary,
									before, 0));
						}
						isCollectingWord = false;
					}
					before++;
					after++;
				}
			}
			if (!word.isEmpty()) {
				if (isCollectingWord) {
					alreadyFormedWords.add(new WordOnBoard(word, index1,
							index2, direction, dictionary, before, 0));
				} else {
					alreadyFormedWords.get(alreadyFormedWords.size() - 1).numberOfEmptyCellsAfter = after;
				}
			}

		}

		// vertical words

		for (int j = 0; j < this.board[0].length; j++) {
			String word = "";

			int index1 = 0, index2 = 0, before = 0, after = 0;
			char direction = 0;
			boolean isCollectingWord = false;

			for (int i = 0; i < this.board.length; i++) {
				if (board[i][j] != null && board[i][j].letter != null) {
					if (!isCollectingWord) {
						if (!word.isEmpty()) {
							alreadyFormedWords
									.get(alreadyFormedWords.size() - 1).numberOfEmptyCellsAfter = after;
						}
						before = 0;
						after = 0;
						index1 = i;
						index2 = j;
						direction = 'v';
						isCollectingWord = true;
						word = "";
					}
					word += board[i][j].letter.symbol;
				} else {
					if (isCollectingWord) {
						if (!word.isEmpty()) {
							alreadyFormedWords.add(new WordOnBoard(word,
									index1, index2, direction, dictionary,
									before, 0));
						}
						isCollectingWord = false;
					}
					before++;
					after++;
				}
			}
			if (!word.isEmpty()) {
				if (isCollectingWord) {
					alreadyFormedWords.add(new WordOnBoard(word, index1,
							index2, direction, dictionary, before, 0));
				} else {
					alreadyFormedWords.get(alreadyFormedWords.size() - 1).numberOfEmptyCellsAfter = after;
				}
			}
		}

		return alreadyFormedWords;
	}

	public void printBoardLetters(Square[][] boardCell) {
		// ill print the BOARD
		System.out.println("---BOARD-----CONTENTS*---");

		for (int i = 0; i < boardCell.length; i++) {
			if (boardCell[i][0].letter != null) {
				System.out.println(boardCell[i][0].letter.symbol);
			}
			for (int j = 0; j < boardCell[i].length; j++) {
				if (boardCell[i][j].letter != null) {
					System.out.println(boardCell[i][j].letter.symbol);
				}
			}

		}
		System.out.println("END OF---BOARD-----CONTENTS*---");
	}

	// get perms
	private void checkWords(FileReader dict) throws IOException {
		BufferedReader bf = new BufferedReader(dict);
		String line = "";
		while ((line = bf.readLine()) != null) {
			if (hasWord(line, getRackasString())) {
				this.words.add(line);
			}
		}
		bf.close();
		dict.close();
	}

	public boolean hasWord(String word, String copy) {

		for (int u = 0; u < word.length(); u++) {
			if (copy.contains(String.valueOf(word.charAt(u)))) {
				copy = copy.replaceFirst(String.valueOf(word.charAt(u)), "");
			} else {
				return false;
			}
		}
		return true;
	}

	/**
	 * returns all the valid permutaions of the rack tiles in the dictonary
	 * 
	 * @return
	 */
	public LinkedList<String> getValidPemutations() {
		LinkedList<String> perms = new LinkedList<String>();

		try {
			checkWords(new FileReader(
					"C://Users//XtarX//Desktop//jamescookie-Scrabble-6b40d2c//ArabicScrabble//src//arabicScrabble//arabicDict.txt"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < words.size(); i++) {
			perms.add(words.get(i));
		}
		return perms;
	}

	// end of perms
	/*
	 * see the postions wher ei can place tiles
	 */
	/**
	 * checks to see if square is !Vaccant(Letter,boarder) or vaccant
	 */
	public boolean isFree(Square s) {
		return !s.used && s.letter != null;
	}

	public LinkedList<Word> analyseBoard(Square[][] boardCell) {
		LinkedList<Word> possible_words = new LinkedList<Word>();
		for (int i = 0; i < boardCell.length; i++) {
			for (int j = 0; j < boardCell[i].length; j++) {
				if (boardCell[i][j].letter != null) {
					// vaccant from left&&right
					// if boarder on left && vaccant form right
					if (j == 0 && boardCell[i][j + 1].letter == null) {
						possible_words.add(new Word(new Point(i, j), "hr", 's',
								boardCell[i][j].letter));
					}
					// if boarder on right && vaccant form left
					if (j == boardCell[i].length - 1
							&& boardCell[i][j - 1].letter == null) {
						possible_words.add(new Word(new Point(i, j), "hl", 'e',
								boardCell[i][j].letter));
					}
					// if boarder on top && vaccant form down
					if (i == 0 && boardCell[i + 1][j].letter == null) {
						possible_words.add(new Word(new Point(i, j), "vd", 's',
								boardCell[i][j].letter));
					}
					// if vaccant form both left &&right
					if (boardCell[i][j - 1].letter == null
							&& boardCell[i][j + 1].letter == null && j > 0
							&& j < 14) {
						possible_words.add(new Word(new Point(i, j), "h", 's',
								boardCell[i][j].letter));
					}
					// if vaccant form both top &&bottom
					if (boardCell[i - 1][j].letter == null
							&& boardCell[i + 1][j].letter == null && i > 0
							&& i < 14) {
						possible_words.add(new Word(new Point(i, j), "v", 's',
								boardCell[i][j].letter));
					}
				}
			}
		}
		return possible_words;
	}

	public LinkedList<String> findwordwith(int size, char s, char letter) {
		LinkedList<String> perms = getValidPemutations();
		LinkedList<String> listofwords = new LinkedList<String>();
		for (int i = 0; i < perms.size(); i++) {
			if (s == 's') {// starts with l
				if (perms.get(i).charAt(0) == letter
						&& perms.get(i).length() <= size) {
					String word = perms.get(i).substring(1);
					listofwords.add(word);
					break;
				}
			}
			if (s == 'e') {// ends with l
				if (perms.get(i).charAt(perms.get(i).length() - 1) == letter
						&& perms.get(i).length() <= size) {
					// String word =
					// perms.get(i).substring(perms.get(i).length());
					String word = perms.get(i).substring(0,
							perms.get(i).length() - 1);
					listofwords.add(word);
					break;
				}

			}

		}
		return listofwords;

	}

	public void findPermthat(Word word, Square[][] boardCell) {
		boolean found = false;
		if (word.orientaion.equals("hr")) {
			int size = 0;
			// get the size of the word to be placed
			for (int i = word.position.y + 1; i < 15; i++) {

				if (boardCell[word.position.x][i].letter == null
						&& boardCell[word.position.x][i + 1].letter == null
						&& i < 15) {
					size++;
				}

			}
			// find a word that starts with L & of lenght <=size
			LinkedList<String> listofPCwords = findwordwith(size, 's',
					word.l.symbol);

			if (listofPCwords.size() == 0) {// not found ie)pass
				System.out.println("PASS");
			} else {
				// we found words
				found = true;
				khalasfound = true;
				System.out.println("found " + listofPCwords.getFirst());

				// place em on board & remove from rack
				String word2place = listofPCwords.getFirst();
				for (int i = 0; i < word2place.length(); i++) {
					// get the letter obj form the rack
					for (int j = 0; j < this.rack.letters.size(); j++) {
						if (this.rack.letters.get(j).symbol == word2place
								.charAt(i)) {
							// add tile to board
							Letter l = this.rack.letters.get(j);
							boardCell[word.position.x][word.position.y + 1 + i]
									.add(l);
							// remove from rack
							this.rack.letters.remove(l);
							break;
						}
					}
				}
				// end of placing & removing from rack
			}
		}
		// case hl
		if (word.orientaion.equals("hl") && !found) {
			int size = 0;
			// get the size of the word to be placed
			for (int i = word.position.y - 1; i > 0; i--) {
				if (boardCell[word.position.x][i].letter == null
						&& boardCell[word.position.x][i - 1].letter == null
						&& i - 1 > 0) {
					size++;
				}

			}
			// find a word that starts with L & of lenght <=size
			LinkedList<String> listofPCwords = findwordwith(size, 's',
					word.l.symbol);

			if (listofPCwords.size() == 0) {// not found ie)pass
				System.out.println("PASS");
			} else {
				// we found words
				found = true;
				khalasfound = true;
				System.out.println("found " + listofPCwords.getFirst());
				// place em on board & remove from rack
				String word2place = listofPCwords.getFirst();
				for (int i = 0; i < word2place.length(); i++) {
					// get the letter obj form the rack
					for (int j = 0; j < this.rack.letters.size(); j++) {
						if (this.rack.letters.get(j).symbol == word2place
								.charAt(i)) {
							// add tile to board
							Letter l = this.rack.letters.get(j);
							boardCell[word.position.x][word.position.y
									- word2place.length() + i].add(l);
							// remove from rack
							this.rack.letters.remove(l);
							break;
						}
					}
				}
				// end of placing & removing from rack

			}
		}
		// case hl
		// case H
		if (word.orientaion.equals("h") && !found) {

			LinkedList<String> listofPCwords = new LinkedList<String>();
			LinkedList<String> listofPCwords2 = new LinkedList<String>();

			int size = 0;
			// get the size of the word to be placed
			for (int i = word.position.y + 1; i < 14; i++) {

				if (boardCell[word.position.x][i].letter == null
						&& boardCell[word.position.x][i + 1].letter == null
						&& i < 13) {
					size++;
				}

			}
			listofPCwords = findwordwith(size, 's', word.l.symbol);

			if (listofPCwords.size() == 0) {// not found ie)pass
				System.out.println("PASS");
			} else {
				// we found words
				found = true;
				khalasfound = true;
				System.out.println("found  H  --> " + listofPCwords.getFirst());
				// place em on board & remove from rack
				String word2place = listofPCwords.getFirst();
				for (int i = 0; i < word2place.length(); i++) {
					// get the letter obj form the rack
					for (int j = 0; j < this.rack.letters.size(); j++) {
						if (this.rack.letters.get(j).symbol == word2place
								.charAt(i)) {
							// add tile to board
							Letter l = this.rack.letters.get(j);
							boardCell[word.position.x][word.position.y + 1 + i]
									.add(l);
							// remove from rack
							this.rack.letters.remove(l);
							break;
						}
					}
				}
				// end of placing & removing from rack

			}
			// handle the case , when the letter ends with L
			if (!found) {
				size = 0;
				// get the size of the word to be placed
				for (int i = word.position.y - 1; i > 0; i--) {
					if (boardCell[word.position.x][i].letter == null
							&& boardCell[word.position.x][i - 1].letter == null
							&& i - 1 > 0) {
						size++;
					}

				}

				// find a word that starts with L & of lenght <=size
				listofPCwords2 = findwordwith(size, 'e', word.l.symbol);

				// concate both lists
				// for (int i = 0; i < listofPCwords2.size(); i++) {
				// listofPCwords.add(listofPCwords2.get(i));
				// }
				// end of concat
				if (listofPCwords2.size() == 0) {// not found ie)pass
					System.out.println("PASS");
				} else {
					// we found words
					found = true;
					khalasfound = true;
					System.out.println("found    <---H "
							+ listofPCwords2.getFirst());
					// place em on board & remove from rack
					String word2place = listofPCwords2.getFirst();
					for (int i = 0; i < word2place.length(); i++) {
						// get the letter obj form the rack
						for (int j = 0; j < this.rack.letters.size(); j++) {
							if (this.rack.letters.get(j).symbol == word2place
									.charAt(i)) {
								// add tile to board
								Letter l = this.rack.letters.get(j);
								boardCell[word.position.x][word.position.y
										- word2place.length() + i].add(l);
								// remove from rack
								this.rack.letters.remove(l);
								break;
							}
						}
					}
					// end of placing & removing from rack

				}
			}
		}
		// end of Case H

		// case V
		if (word.orientaion.equals("v") && !found
				|| word.orientaion.equals("vd") && !found) {
			LinkedList<String> listofPCwords = new LinkedList<String>();
			int size = 0;
			// get the size of the word to be placed
			for (int i = word.position.x + 1; i < 14; i++) {
				if (boardCell[i][word.position.y].letter == null
						&& boardCell[word.position.x][i + 1].letter == null
						&& i < 13) {
					size++;
				}

			}
			listofPCwords = findwordwith(size, 's', word.l.symbol);
			// handle the case , when the letter ends with L
			if (listofPCwords.size() == 0) {// not found ie)pass
				System.out.println("PASS");
			} else {
				// we found words
				found = true;
				khalasfound = true;
				System.out.println("found    Verically "
						+ listofPCwords.getFirst());

				// place em on board & remove from rack
				String word2place = listofPCwords.getFirst();
				for (int i = 0; i < word2place.length(); i++) {
					// get the letter obj form the rack
					for (int j = 0; j < this.rack.letters.size(); j++) {
						if (this.rack.letters.get(j).symbol == word2place
								.charAt(i)) {
							// add tile to board
							Letter l = this.rack.letters.get(j);
							boardCell[word.position.x + 1 + i][word.position.y]
									.add(l);
							// remove from rack
							this.rack.letters.remove(l);
							break;
						}
					}
				}
				// end of placing & removing from rack

				// for (int i = 0; i < listofPCwords.size(); i++) {
				// System.out.println("found # " + i + " : "
				// + listofPCwords.get(i));
				// }
			}
		}
		// end of Case V

	}

	public Square[][] pcPlay(Square[][] boardCell)
			throws FileNotFoundException, IOException {
		khalasfound = false;
		LinkedList<Word> possible_words = analyseBoard(boardCell);

		System.out.println("---  BEIGGGG-----------");

		for (int i = 0; i < possible_words.size(); i++) {

			System.out.println("----------------------");
			System.out.println(possible_words.get(i).l.symbol);

			System.out.println(possible_words.get(i).position.x + " & "
					+ possible_words.get(i).position.y);

			System.out.println(possible_words.get(i).orientaion + " & "
					+ possible_words.get(i).wordPosition);
			System.out.println("----------------------");
		}
		System.out.println("---  ENNND-----------");

		/**
		 * LinkedList<String> perms = getValidPemutations(); boolean found =
		 * false; String word_pc = "";// the word that the pc will play if yes.
		 */
		// START OF AI

		for (int i = 0; i < possible_words.size(); i++) {
			if (!khalasfound) {
				findPermthat(possible_words.get(i), boardCell);
			}
		}
		return boardCell;

	}

	public void evaluateBoard() {

	}

	public void generateMove() {

	}

}
